package com.grf.hadoop.mul_job;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class MulJobMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private String fileName;
    private IntWritable v = new IntWritable(1);
    private Text k = new Text();

    @Override
    protected void setup(Context context)
            throws IOException, InterruptedException {
        //获取文件名称
        FileSplit inputSplit = (FileSplit)context.getInputSplit();
        fileName = inputSplit.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        String line = value.toString();
        String[] fields =  line.split(" ");
        for (String word :fields) {
            k.set(word+ "--" + fileName);
            context.write(k, v);
        }

    }
}
